import {reactive} from "vue";

const validateUsername = (rule, value, callback) => {

    const usernameRegex = /^[a-zA-Z0-9_]+$/;
    console.log(value, usernameRegex.test(value))
    if (!usernameRegex.test(value)) {
        callback(new Error('只允许字母、数字、和下划线（_）'))
    } else {
        callback()
    }
}

const validatePassword = (rule, value, callback) => {

    const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{5,20}$/;
    console.log(value, passwordRegex.test(value))
    if (!passwordRegex.test(value)) {
        callback(new Error('至少一个小写字母、一个大写字母和一个数字'))
    } else {
        callback()
    }
}

export const loginFormRules = reactive({
    username: [
        {validator: validateUsername, trigger: 'change'},
        {required: true, message: '用户名为空', trigger: 'blur'},
        {min: 5, max: 20, message: '用户名不在范围内（5~20）', trigger: 'change'},
    ],
    password: [
        {min: 5, max: 20, message: '密码不在范围内（5~20）', trigger: 'change'},
        {validator: validatePassword, trigger: 'change'},
    ]
});